package com.ycsj.goldmedalnewconcept.teacher.fragment;

import android.os.Bundle;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.timmy.tdialog.TDialog;
import com.timmy.tdialog.base.BindViewHolder;
import com.timmy.tdialog.listener.OnBindViewListener;
import com.timmy.tdialog.listener.OnViewClickListener;
import com.ycsj.common.base.BaseLazyFragment;
import com.ycsj.common.base.BusInfo;
import com.ycsj.common.manager.TDialogManager;
import com.ycsj.common.utils.MaxTextLengthFilter;
import com.ycsj.common.utils.UiUtils;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.TkKeys;
import com.ycsj.goldmedalnewconcept.common.bean.SubListBean;
import com.ycsj.goldmedalnewconcept.common.bean.TkItemBean;
import com.ycsj.goldmedalnewconcept.common.util.AccountUtils;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;

/**
 * 翻译题
 * Created by Administrator on 2018/9/3 0003.
 */

public class TranslateFragment extends BaseLazyFragment implements View.OnClickListener {

    //题目类型
    @BindView(R.id.tv_type)
    TextView tvType;
    //题干
    @BindView(R.id.tv_content)
    TextView tvContent;
    //显示输入答案的TextView
    @BindView(R.id.tv_show_answer)
    TextView tvShowAnswer;
    //作答
    @BindView(R.id.tv_own_answer)
    TextView tvOwnAnswer;
    //正确答案
    @BindView(R.id.tv_right_answer)
    TextView tvRightAnswer;
    //解析
    @BindView(R.id.tv_parse_answer)
    TextView tvParseAnswer;
    //作答,正确答案,解析的整体布局
    @BindView(R.id.ll_show_answer)
    LinearLayout llShowAnswer;

    @BindView(R.id.ll_stu_answer)
    LinearLayout llStuAnswer;

    //全校正确率的整体布局
    @BindView(R.id.ll_qxzql)
    LinearLayout llQxzql;

    //全校正确率的TextView
    @BindView(R.id.tv_qxzql)
    TextView tvQxzql;

    private TkItemBean info;

    //页面对象
    private SubListBean subListBean;

    //学生做题0  1学生查看解析 2老师查看题和解析-有人做(全校正确率和小题正确率都显示) 3(习题分析)老师查看题和解析-没人做,对应查看单题
    private String type;

    //当前大题在整个题目中所处的位置
    private int currPos;

    public static final TranslateFragment newInstance(TkItemBean item, String type,int currPos) {
        TranslateFragment teacherGoldFragment = new TranslateFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("data", item);
        bundle.putSerializable("type", type);
        bundle.putInt("currpos", currPos);
        teacherGoldFragment.setArguments(bundle);
        return teacherGoldFragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_tk_translate;
    }

    @Override
    public void lazyInit(View view, Bundle savedInstanceState) {

    }

    @Override
    protected void init(View view, Bundle savedInstanceState) {
        super.init(view, savedInstanceState);

        //获取页面对象
        initIntent();

        //初始化视图
        initView();
    }


    /**
     * 获取页面对象
     */
    private void initIntent() {

        info = (TkItemBean) getArguments().getSerializable("data");

        subListBean = info.getSub_list().get(0);

        type = (String) getArguments().getSerializable("type");

        //获取当前大题在整个题目中所处的位置
        currPos = getArguments().getInt("currpos");
    }

    /**
     * 初始化页面状态视图
     */
    private void initView() {
        if ("0".equals(type)) {
            //学生未答题
            llShowAnswer.setVisibility(View.GONE);
            //未完成状态下设置页面数据,包含处理题目的显示以及点击事件的处理等
            initNoCompleteData();
        } else if ("1".equals(type)) {
            //学生已答题查看解析
            llShowAnswer.setVisibility(View.VISIBLE);
            llStuAnswer.setVisibility(View.VISIBLE);
            llQxzql.setVisibility(View.GONE);
            initHasCompletedData();
        } else if ("2".equals(type)) {
            //老师查看学生解析(有人做)
            llShowAnswer.setVisibility(View.VISIBLE);
            llStuAnswer.setVisibility(View.GONE);
            llQxzql.setVisibility(View.VISIBLE);
            initHasCompletedData();

        } else if ("3".equals(type)) {
            //查看单题或老师查看学生解析(没人做)
            llShowAnswer.setVisibility(View.VISIBLE);
            llStuAnswer.setVisibility(View.GONE);
            llQxzql.setVisibility(View.GONE);
            initHasCompletedData();
        }

        //获取标题的内容
        if (TextUtils.isEmpty(info.getN_topic_name())) {
            tvType.setText(info.getN_type_name());
        } else {
            tvType.setText(info.getN_type_name() + "-" + info.getN_topic_name());
        }
    }

    /**
     * 做题状态下设置数据
     */
    private void initNoCompleteData() {
        //设置题干
        setQuestionContent();

        //初次进入设置显示答案的TextView的布局参数
        setTvAnswerLayoutParams();

        if("0".equals(AccountUtils.getRole(getActivity()))){
            //未完成列表选项可以点击
            //设置点击事件
            tvShowAnswer.setOnClickListener(this);
        }else{
            //家长不可点击
        }

    }

    /**
     * 查看状态下设置数据
     */
    private void initHasCompletedData() {
        //设置题干
        setQuestionContent();

        if ("1".equals(type)) {
            //设置显示答案的TextView的布局参数
            setTvAnswerLayoutParams();

            //完成状态下设置用户的作答内容
            //设置作答,正确答案,以及解析
            if (!TextUtils.isEmpty(subListBean.getStu_option())) {
                tvShowAnswer.setText(subListBean.getStu_option());
                tvOwnAnswer.setText(subListBean.getStu_option());
            } else {
                tvShowAnswer.setText("未作答");
                tvOwnAnswer.setText("未作答");
            }
        }


        //设置正确答案
        if (null != info.getSub_list()) {
            tvRightAnswer.setText(subListBean.getOption());
        }

        if ("2".equals(type)) {

            //全校正确率
            tvQxzql.setText(subListBean.getYes_pro());
        }

        //设置解析
        tvParseAnswer.setText(info.getAnalysis());
    }


    /**
     * 设置题干的方法
     */
    private void setQuestionContent() {
        if (info != null) {
            tvContent.setText(info.getContent());
        }
    }

    /**
     * 设置显示答案的TextView的布局参数
     */
    private void setTvAnswerLayoutParams() {
        int padding = UiUtils.dp2px(4);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        tvShowAnswer.setPadding(padding, padding, padding, padding);
        tvShowAnswer.setBackgroundResource(R.drawable.question_tv_out_bg);
        tvShowAnswer.setLayoutParams(lp);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tv_show_answer) {
            //展示答案的TextView的点击事件
            showInputDialog();
        }
    }


    /**
     * 弹出输入框
     */
    private void showInputDialog() {
        TDialogManager.show(getActivity(),getActivity().getSupportFragmentManager(), R.layout.teacher_app_pop_input_read_translate, 0.3f, new OnBindViewListener() {
            @Override
            public void bindView(BindViewHolder viewHolder) {
                viewHolder.setText(R.id.tv_pop_content, "请输入答案");
                EditText etInput = viewHolder.getView(R.id.et_input);
                etInput.setFilters(new InputFilter[]{new MaxTextLengthFilter(151)});
            }
        }, new OnViewClickListener() {
            @Override
            public void onViewClick(BindViewHolder viewHolder, View view, TDialog tDialog) {
                switch (view.getId()) {
                    case R.id.tv_cancel:
                        TDialogManager.dismiss();
                        break;
                    case R.id.tv_ok:
                        EditText etInput = viewHolder.getView(R.id.et_input);
                        //第一步:获取输入框的文字,进行非空判断
                        String content = etInput.getText().toString().trim();
                        if (TextUtils.isEmpty(content)) {
                            //没有输入单词
                            UiUtils.Toast("您还没有输入单词", false);
                            return;
                        } else {
                            //已经输入了单词
                            //第二步:将输入的单词回显到展示答案的TextView上
                            tvShowAnswer.setText(content);
                            setTvAnswerLayoutParams();

                            //通过EventBs将用户作答本小题的相关信息传递给TkMainActivity保存
                            //currPos对应的每个大题的小题的位置
                            int choosePos = 0;

                            //用户选的输入的单词
                            String chooseAnswer = content;
                            //是否如正确
                            boolean chooseResult = true;
//                            if (content.equals()) {
//                                //用户作答正确
//                                chooseResult = true;
//                            } else {
//                                //用户作答错误
//                                chooseResult = false;
//                            }
                            BusInfo busInfo = new BusInfo();
                            busInfo.tkMsg = TkKeys.TKMsg;
                            busInfo.tkChoosePos = choosePos;
                            busInfo.tkChooseAnswer = chooseAnswer;
                            busInfo.tkChooseResult = chooseResult;
                            EventBus.getDefault().post(busInfo);

                            //关闭对话框
                            TDialogManager.dismiss();
                        }

                        break;
                }
            }
        }, R.id.tv_cancel, R.id.tv_ok);
    }
}

